﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using Facade.Excel;

namespace PeisongWeb.admin
{
    public partial class currentSupplyPrice : adminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("CD", 1))
                {
                    drpBind();
                    businessNatureBind();
                    rptBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }

        private void drpBind()
        {
            drp_CategoryRoot.DataSource = logic.category.getByParent(0);
            drp_CategoryRoot.DataBind();
            drp_CategoryRoot.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择大类--", ""));
            //categorySubBind();
        }
        void categorySubBind()
        {
            if (drp_CategoryRoot.SelectedValue != "")
            {
                DataTable dt = logic.category.getByParent(Convert.ToInt32(drp_CategoryRoot.SelectedValue));
                drp_CategorySub.DataSource = dt;
                drp_CategorySub.DataBind();
                drp_CategorySub.Items.Insert(0, new ListItem("-请选择小类-", ""));
            }
        }

        void businessNatureBind()
        {
            DataTable dt = logic.sysCode.buyerNatureList();
            ddl_BusinessNature.DataSource = dt;
            ddl_BusinessNature.DataBind();
            ddl_BusinessNature.Items.Insert(0, new ListItem("-所有等级-", ""));
        }

        void rptBind()
        {
            StringBuilder where = new StringBuilder();
            where.Append("1=1");
            if (!string.IsNullOrEmpty(drp_CategoryRoot.SelectedValue))
                where.AppendFormat(" and parentId={0}", drp_CategoryRoot.SelectedValue);
            if (!string.IsNullOrEmpty(drp_CategorySub.SelectedValue))
                where.AppendFormat(" and categoryId={0}", drp_CategorySub.SelectedValue.ToString().Trim());
            if (!string.IsNullOrEmpty(txtProductName.Text))
                where.AppendFormat(" and productName like'%{0}%'", txtProductName.Text);
            if (!string.IsNullOrEmpty(ddl_BusinessNature.SelectedValue))
                where.AppendFormat(" and businessNature='{0}'", ddl_BusinessNature.SelectedValue);

            DataSet ds = logic.price.selectSupplyPriceList(where.ToString(), "productId,price", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);

            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
        }
        protected void ddl_CategoryRoot_SelectedIndexChanged(object sender, EventArgs e)
        {
            categorySubBind();
            rptBind();
        }

        protected void ddl_CategorySub_SelectedIndexChanged(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            rptBind();
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }

        // 导出全部商品
        protected void lbtn_ToExeclAll_Click(object sender, EventArgs e)
        {
            DataSet ds = logic.price.getSupplyPriceList();

            if (ds.Tables.Count > 1)
            {
                string title = string.Format("商品当前供应价格列表【{0}】", DateTime.Now.ToShortDateString());

                DataTable tb1 = ds.Tables[0];
                Dictionary<string, string> sheets = new Dictionary<string, string>();
                for (int i = 0; i < tb1.Rows.Count; i++)
                {
                    sheets.Add(tb1.Rows[i][0].ToString(), tb1.Rows[i][1].ToString());
                }
                DataTable tb2 = ds.Tables[1];

                Dictionary<string, string> columns = new Dictionary<string, string>();
                columns.Add("productNo", "商品编号");
                columns.Add("productName", "商品名称");
                columns.Add("unit", "单位");
                columns.Add("standard", "规格");
                columns.Add("company", "供应商名称");
                //columns.Add("sellerPrice", "供应价格");
                if (cbo_hasSellerPrice.Checked)
                {
                    columns.Add("sellerPrice", "供应价格");
                }
                if (cbo_hasPrice.Checked)
                {
                    columns.Add("Price", "系统价格");
                }

                Dictionary<int, FormatStyle> formatOptions = new Dictionary<int, FormatStyle>();
                formatOptions.Add(5, FormatStyle.ToFix2);

                ExportFacade exportHelper = new ExportFacade();
                exportHelper.ExportByWeb("businessNature='{0}'", sheets, columns, tb2, title, title + ".xls", formatOptions, title);
            }
        }

        // 导出当前列表商品
        protected void lbtn_ToExecl_Click(object sender, EventArgs e)
        {
            StringBuilder where = new StringBuilder("1=1");
            if (!string.IsNullOrEmpty(drp_CategoryRoot.SelectedValue))
                where.AppendFormat(" and parentId={0}", drp_CategoryRoot.SelectedValue);
            if (!string.IsNullOrEmpty(drp_CategorySub.SelectedValue))
                where.AppendFormat(" and categoryId={0}", drp_CategorySub.SelectedValue);
            if (!string.IsNullOrEmpty(ddl_BusinessNature.SelectedValue))
                where.AppendFormat(" and businessNature='{0}'", ddl_BusinessNature.SelectedValue);

            //如果选了[大类]和[档次]两项,导出某一个大类的Excel,其中的小类按sheet来分类
            if (!string.IsNullOrEmpty(drp_CategoryRoot.SelectedValue)&&!string.IsNullOrEmpty(ddl_BusinessNature.SelectedValue))
            {
                string cols = "categoryId,categoryName,productNo,productName,unit,standard,company";
                string bystr = "productId";
                if (cbo_hasSellerPrice.Checked)
                {
                    cols += ",sellerPrice";
                }
                if (cbo_hasPrice.Checked)
                {
                    cols += ",Price";
                    bystr+=",price";
                }
                DataTable supplyPriceTab = logic.price.selectSupplyPriceListFilterCols(cols, where.ToString(), bystr);

                Dictionary<string, string> sheets = GetCategorySheetList(supplyPriceTab);
                string title = string.Format(drp_CategoryRoot.SelectedItem.Text+"商品信息列表【{0}】", DateTime.Now.ToShortDateString());
                Dictionary<string, string> columns = new Dictionary<string, string>();
                columns.Add("categoryName", "所属小类");
                columns.Add("productNo", "商品编号");
                columns.Add("productName", "商品名称");
                columns.Add("unit", "单位");
                columns.Add("standard", "规格");
                columns.Add("company", "供应商名称");
                if (cbo_hasSellerPrice.Checked)
                {
                    columns.Add("sellerPrice", "供应价格");
                }
                if (cbo_hasPrice.Checked)
                {
                    columns.Add("Price", "系统价格");
                }
                Dictionary<int, FormatStyle> formatOptions = new Dictionary<int, FormatStyle>();
                formatOptions.Add(5, FormatStyle.ToFix2);

                ExportFacade exportHelper = new ExportFacade();
                exportHelper.ExportByWeb("categoryId='{0}'", sheets, columns, supplyPriceTab, title, title + ".xls", formatOptions, title);
            }
            else
            {
                string cols = " productNo,businessNature,businessNatureName,productName,unit,standard,company";
                string bystr = "productId";
                if (cbo_hasSellerPrice.Checked)
                {
                    cols += ",sellerPrice";
                }
                if (cbo_hasPrice.Checked)
                {
                    cols += ",Price";
                    bystr += ",price";
                }
                DataTable supplyPriceTab = logic.price.selectSupplyPriceListFilterCols(cols, where.ToString(),bystr);

                Dictionary<string, string> sheets = GetSheetList(supplyPriceTab);
                string title = string.Format("商品当前供应价格列表【{0}】", DateTime.Now.ToShortDateString());
                Dictionary<string, string> columns = new Dictionary<string, string>();
                columns.Add("productNo", "商品编号");
                columns.Add("productName", "商品名称");
                columns.Add("unit", "单位");
                columns.Add("standard", "规格");
                columns.Add("company", "供应商名称");
                if (cbo_hasSellerPrice.Checked)
                {
                    columns.Add("sellerPrice", "供应价格");
                }
                if (cbo_hasPrice.Checked)
                {
                    columns.Add("Price", "系统价格");
                }

                Dictionary<int, FormatStyle> formatOptions = new Dictionary<int, FormatStyle>();
                formatOptions.Add(5, FormatStyle.ToFix2);

                ExportFacade exportHelper = new ExportFacade();
                exportHelper.ExportByWeb("businessNature='{0}'", sheets, columns, supplyPriceTab, title, title + ".xls", formatOptions, title);
            }
        }

        protected void link_Search_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            rptBind();
        }

        protected void ddl_BusinessNature_SelectedIndexChanged(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            rptBind();
        }
        private Dictionary<string, string> GetCategorySheetList(DataTable dt)
        {
            Dictionary<string, string> sheets = new Dictionary<string, string>();

            DataTable newTable = dt.DefaultView.ToTable(true, new string[2] { "categoryId", "categoryName" });
            for (int i = 0; i < newTable.Rows.Count; i++)
            {
                sheets.Add(newTable.Rows[i][0].ToString(), newTable.Rows[i][1].ToString());
            }
            return sheets;
        }

        private Dictionary<string, string> GetSheetList(DataTable dt)
        {
            Dictionary<string, string> sheets = new Dictionary<string, string>();

            DataTable newTable = dt.DefaultView.ToTable(true, new string[2] { "businessNature", "businessNatureName" });
            for (int i = 0; i < newTable.Rows.Count; i++)
            {
                sheets.Add(newTable.Rows[i][0].ToString(), newTable.Rows[i][1].ToString());
            }
            return sheets;
        }
    }
}
